package com.stylefeng.guns.modular.bus.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.util.KaptchaUtil;
import com.stylefeng.guns.core.util.OSSUtil;
import com.stylefeng.guns.modular.system.model.Dept;
import com.stylefeng.guns.modular.system.service.IDeptService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.BusPrductInfo;
import com.stylefeng.guns.modular.bus.service.IBusPrductInfoService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * product控制器
 *
 * @author fengshuonan
 * @Date 2018-04-21 01:16:06
 */
@Controller
@RequestMapping("/busPrductInfo")
public class BusPrductInfoController extends BaseController {

    private String PREFIX = "/bus/busPrductInfo/";

    @Autowired
    private IBusPrductInfoService busPrductInfoService;
    @Autowired
    private IDeptService deptService;

    /**
     * 跳转到product首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "busPrductInfo.html";
    }

    /**
     * 跳转到添加product
     */
    @RequestMapping("/busPrductInfo_add")
    public String busPrductInfoAdd() {
        return PREFIX + "busPrductInfo_add.html";
    }

    /**
     * 跳转到修改product
     */
    @RequestMapping("/busPrductInfo_update/{busPrductInfoId}")
    public String busPrductInfoUpdate(@PathVariable String busPrductInfoId, Model model) {
        BusPrductInfo busPrductInfo = busPrductInfoService.selectById(busPrductInfoId);
        Dept dept=deptService.selectById(busPrductInfo.getComId());
        busPrductInfo.setComName(dept.getSimplename());
        busPrductInfo.setPrdImgFilePath(OSSUtil.getFileUrl(busPrductInfo.getPrdImgFilename(),99999));
        model.addAttribute("item",busPrductInfo);
        LogObjectHolder.me().set(busPrductInfo);
        return PREFIX + "busPrductInfo_edit.html";
    }

    /**
     * 获取product列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String prdName) {
        Map<String,String > map=new HashMap();
        map.put("prdName",prdName);
        Dept dept=deptService.selectById(ShiroKit.getUser().getDeptId());
        Integer comPid=dept.getPid();
        KaptchaUtil.comAuth(map,comPid,String.valueOf(ShiroKit.getUser().getDeptId()));
        List<BusPrductInfo> list=busPrductInfoService.getProductList4ComId(map);
        for (int i = 0; i < list.size(); i++) {
            BusPrductInfo busPrductInfo=list.get(i);
            busPrductInfo.setPrdImgFilePath(OSSUtil.getFileUrl(busPrductInfo.getPrdImgFilename(),99999));
        }
        return list;
    }

    /**
     * 获取product列表
     */
    @RequestMapping(value = "/list4ComId")
    @ResponseBody
    public Object list4ComId(@RequestParam("comId") String comId) {
        Map map=new HashMap();
        map.put("comId",comId);
        List<BusPrductInfo> list=busPrductInfoService.getProductList4ComId(map);
        for (int i = 0; i < list.size(); i++) {
            BusPrductInfo busPrductInfo=list.get(i);
            busPrductInfo.setPrdImgFilePath(OSSUtil.getFileUrl(busPrductInfo.getPrdImgFilename(),99999));
        }
        return list;
    }

    /**
     * 新增product
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(BusPrductInfo busPrductInfo) {
        busPrductInfoService.insert(busPrductInfo);
        return SUCCESS_TIP;
    }

    /**
     * 删除product
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam String busPrductInfoId) {
        busPrductInfoService.deleteById(busPrductInfoId);
        return SUCCESS_TIP;
    }

    /**
     * 修改product
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(BusPrductInfo busPrductInfo) {
        busPrductInfoService.updateById(busPrductInfo);
        return SUCCESS_TIP;
    }

    /**
     * product详情
     */
    @RequestMapping(value = "/detail/{busPrductInfoId}")
    @ResponseBody
    public Object detail(@PathVariable("busPrductInfoId") Integer busPrductInfoId) {
        BusPrductInfo busPrductInfo=busPrductInfoService.selectById(busPrductInfoId);
        busPrductInfo.setPrdImgFilePath(OSSUtil.getFileUrl(busPrductInfo.getPrdImgFilename(),99999));
        return busPrductInfo;
    }

}
